﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TuanAnhPersonalWeb.Models;

namespace TuanAnhPersonalWeb.Classes
{
    public class ProjectAccessConsolidator
    {
        public TuanAnhEntities db = new TuanAnhEntities();
        public IList<ProjectModel> getAllProject()
        {
            IList<ProjectModel> list = new List<ProjectModel>();
            var data = db.TUANANH__PROJECT.ToList();
            foreach (var item in data)
            {
                ProjectModel model = new ProjectModel();
                model.PROJECT_ID = item.PROJECT_ID;
                model.CREATEDATE = item.CREATEDATE;
                model.IMAGE_DIR = item.IMAGE_DIR;
                model.INVERSTER = item.INVERSTER;
                model.LINK = item.LINK;
                model.LOCATION = item.LOCATION;
                model.OTHER = item.OTHER;
                model.PROJECT_CONTENT = item.PROJECT_CONTENT;
                model.PROJECT_NAME = item.PROJECT_NAME;
                model.PTYPE = item.PTYPE;
                model.PDESCRIPTION = item.TUANANH__PROJECTTYPE.PDESCRIPTION;
                list.Add(model);
            }
            return list;

        }
        public ProjectModel getProjectbyID(int id)
        {
            ProjectModel model = new ProjectModel();
            TUANANH__PROJECT item = db.TUANANH__PROJECT.Single(e => e.PROJECT_ID == id);
            model.PROJECT_ID = item.PROJECT_ID;
            model.CREATEDATE = item.CREATEDATE;
            model.IMAGE_DIR = item.IMAGE_DIR;
            model.INVERSTER = item.INVERSTER;
            model.LINK = item.LINK;
            model.LOCATION = item.LOCATION;
            model.OTHER = item.OTHER;
            model.PROJECT_CONTENT = item.PROJECT_CONTENT;
            model.PROJECT_NAME = item.PROJECT_NAME;
            model.PTYPE = item.PTYPE;
            model.PDESCRIPTION = item.TUANANH__PROJECTTYPE.PDESCRIPTION;
            return model;
        }
        public void updateCode()
        {
            int? Code = db.TUANANH__CODE.Where(e => e.TYPE.Equals("PROJECT")).Select(e => e.CODE).Single();
            TUANANH__CODE model = db.TUANANH__CODE.Single(e => e.CODE == Code);
            model.CODE = Code + 1;
            db.SaveChanges();
        }
        public void insertProject(ProjectModel item)
        {
            int? Code = db.TUANANH__CODE.Where(e => e.TYPE.Equals("PROJECT")).Select(e => e.CODE).Single();
            int key = int.Parse(Code.ToString()) + 1;
            TUANANH__PROJECT model = new TUANANH__PROJECT();
            model.PROJECT_ID = key;
            model.CREATEDATE = DateTime.Now;
            model.IMAGE_DIR = item.IMAGE_DIR;
            model.INVERSTER = item.INVERSTER;
            model.LINK = "Project/DetailProject/?projectId=" + key.ToString();
            model.LOCATION = item.LOCATION;
            model.OTHER = item.OTHER;
            model.PROJECT_CONTENT = item.PROJECT_CONTENT;
            model.PROJECT_NAME = item.PROJECT_NAME;
            model.PTYPE = item.PTYPE;
            db.TUANANH__PROJECT.AddObject(model);
            db.SaveChanges();
            updateCode();
        }
        public void updateProject(ProjectModel item)
        {
        TUANANH__PROJECT model = db.TUANANH__PROJECT.Single(e => e.PROJECT_ID == item.PROJECT_ID);
        model.IMAGE_DIR = item.IMAGE_DIR;
        model.INVERSTER = item.INVERSTER;
        model.LOCATION = item.LOCATION;
        model.OTHER = item.OTHER;
        model.PROJECT_CONTENT = item.PROJECT_CONTENT;
        model.PROJECT_NAME = item.PROJECT_NAME;
        model.PTYPE = item.PTYPE;
        db.SaveChanges();

        }
        public IEnumerable<TUANANH__PROJECTTYPE> getAllPType()
        {
            var data = db.TUANANH__PROJECTTYPE.Select(e => e).ToList();
            return data.Count() > 0 ? data : null;
        }

    }
}